Loading packages
Reading data and setting global parameters
# color palette
cols <- viridis(10)
# ggplot2 theme
theme_set(theme_tree2(base_size = 14,base_family = "Arial"))
# main tree
hmm.swft.cpr.tree <- read.tree("data/processed/trees/consensus_tree_swifts_hummingbirds_and_nighjars_max_cred.tree")
## element acoustic parameters
all.elm.acou.prm <- read.csv("data/processed/acoustic/Element parameters nighjars swifts and hummingbirds.csv", stringsAsFactors = FALSE)
all.elm.acou.prm$species <- gsub(" ", "_", all.elm.acou.prm$Species)
# song acoustic parameters
song.acoust.param <- read.csv("data/processed/acoustic/Song parameters nightjars swifts and hummingbirds.csv", stringsAsFactors = FALSE)
song.acoust.param$species <- song.acoust.param$species_uscr
# # complexity parameters
songs.complx.param <- read.csv("data/processed/acoustic/log10 songs complexity parameters nightjars swifts and hummingbirds.csv", stringsAsFactors = FALSE)
# formula for aggregating at the species level
frmla.elm <- as.formula(paste0("cbind(", paste(names(all.elm.acou.prm)[5:30], collapse = ", "), ") ~ species + Clade"))
frmla.sng <- as.formula(paste0("cbind(", paste(names(song.acoust.param)[c(11:35, 38:40)], collapse = ", "), ") ~ species + Clade"))
frmla.cmplx <- as.formula(paste0("cbind(", paste(names(songs.complx.param)[-1], collapse = ", "), ") ~ species + Clade"))
# aggregate all parameters
agg.elm.param.hmm <- agg.elm.param <- aggregate(frmla.elm, all.elm.acou.prm, mean)
agg.sng.param.hmm <- agg.sng.param <- aggregate(frmla.sng, song.acoust.param, mean)
songs.complx.param$Clade <- sapply(songs.complx.param$species, function(x) unique(all.elm.acou.prm$Clade[all.elm.acou.prm$species == x]))
agg.cmplx.param.hmm <- agg.cmplx.param <- aggregate(frmla.cmplx, songs.complx.param, mean)
# rename hummers
agg.elm.param$Clade <- ifelse(agg.elm.param$Clade %in% c("Nightjars", "Swifts"), agg.elm.param$Clade, "Hummingbirds")
agg.sng.param$Clade <- ifelse(agg.sng.param$Clade %in% c("Nightjars", "Swifts"), agg.sng.param$Clade, "Hummingbirds")
agg.cmplx.param$Clade <- ifelse(agg.cmplx.param$Clade %in% c("Nightjars", "Swifts"), agg.cmplx.param$Clade, "Hummingbirds")
# MRCAs for major clades
major.clade.nodes <- sapply(c("Nightjars", "Swifts", "Hummingbirds"), function(x) getMRCA(phy = hmm.swft.cpr.tree, tip = agg.elm.param$species[agg.elm.param$Clade == x]))
# remove no hummingbirds
agg.elm.param.hmm <- agg.elm.param.hmm[!agg.elm.param.hmm$Clade %in% c("Swifts", "Nightjars", "Patagona"), ]
agg.sng.param.hmm <- agg.sng.param.hmm[!agg.sng.param.hmm$Clade %in% c("Swifts", "Nightjars", "Patagona"), ]
agg.cmplx.param.hmm <- agg.cmplx.param.hmm[!agg.cmplx.param.hmm$Clade %in% c("Swifts", "Nightjars", "Patagona"), ]
# hummingbirds only tree
hmm.tree <- drop.tip(hmm.swft.cpr.tree, tip = setdiff(agg.elm.param$species, agg.elm.param.hmm$species))
# MRCAs for hummingbird subclade (excluding Patagona)
hmm.clade.nodes <- sapply(unique(agg.elm.param.hmm$Clade[agg.elm.param.hmm$Clade != "Patagona"]), function(x) getMRCA(phy = hmm.tree, tip = agg.elm.param.hmm$species[agg.elm.param.hmm$Clade == x]))
hmm.clade.nodes <- hmm.clade.nodes[order(names(hmm.clade.nodes))]
# base ggtree graph major clades
gg.maj.clades <- ggtree(hmm.swft.cpr.tree, ladderize = FALSE) +
theme_tree2() +
theme(text=element_text(size = 20))
# add color boxes
for(i in 1:length(gg.maj.clades))
gg.maj.clades <- gg.maj.clades + geom_hilight(node = major.clade.nodes[i], fill = cols[c(3, 6 ,9)][i])
# base ggtree graph hummingbird clades
gg.hmm.clades <- ggtree(hmm.tree, ladderize = FALSE) +
theme_tree2() +
theme(text=element_text(size = 20))
# color clades
for(i in 1:length(hmm.clade.nodes))
gg.hmm.clades <- gg.hmm.clades + geom_hilight(node = hmm.clade.nodes[i], fill = cols[i + 2])
# tree with 3 tips for the 3 major clades
major.clade.collapse.tree <- drop.tip(hmm.swft.cpr.tree, tip = setdiff(hmm.swft.cpr.tree$tip.label, agg.elm.param$species[!duplicated(agg.elm.param$Clade)]))
# rename tips
major.clade.collapse.tree$tip.label <- c("Nightjars", "Swifts", "Hummingbirds")
# base ggtree graph major clades
gg.major.clade.collapse.tree <- ggtree(major.clade.collapse.tree, ladderize = FALSE) +
theme_tree2() +
theme(text=element_text(size = 20))
# add colors to tips
for(i in 1:Ntip(major.clade.collapse.tree))
gg.major.clade.collapse.tree <- gg.major.clade.collapse.tree + geom_hilight(node = i, fill = cols[c(3 ,6, 9)][i])
gg.major.clade.collapse.tree <- gg.major.clade.collapse.tree +
geom_label(aes(label = label), hjust = 1)
### tree with 8 tips for hummer clades
hmm.clade.collapse.tree <- drop.tip(hmm.swft.cpr.tree, tip = setdiff(hmm.swft.cpr.tree$tip.label, agg.elm.param.hmm$species[!duplicated(agg.elm.param.hmm$Clade)]))
# rename tips
hmm.clade.collapse.tree$tip.label <- c("Hermits", "Topazes", "Mangoes", "Emeralds", "Mtn. Gems", "Bees", "Brilliants", "Coquettes")
# base ggtree graph major clades
gg.hmm.clade.collapse.tree <- ggtree(hmm.clade.collapse.tree, ladderize = FALSE) +
theme_tree2() +
theme(text=element_text(size = 20))
x <- 1
# add colors to tips
for(i in order(hmm.clade.collapse.tree$tip.label, decreasing = FALSE))
{ gg.hmm.clade.collapse.tree <- gg.hmm.clade.collapse.tree + geom_hilight(node = i, fill = cols[3:10][x])
x <- x + 1
}
## add tip labels
gg.hmm.clade.collapse.tree <- gg.hmm.clade.collapse.tree +
geom_label(aes(label = label), hjust = 1)
Element acoustic parameters
for(w in names(agg.elm.param)[-c(1, 2)]){
print(w)
# major clades
gg.elm.prm <- gg.maj.clades
gg.elm.prm <- facet_plot(gg.elm.prm, panel= w, data = agg.elm.param, geom = geom_point, aes(x = get(w), color = Clade)) +
scale_color_manual(values = cols[c(3, 9, 6)]) +
theme(legend.position = c(.15,.8), legend.background = element_rect(fill = "transparent"))
# hummingbird clades
gg.elm.prm.hmm <- gg.hmm.clades
gg.elm.prm.hmm <- facet_plot(gg.elm.prm.hmm, panel = w, data = agg.elm.param.hmm, geom=geom_point, aes(x= get(w), color = Clade)) +
scale_color_manual(values = cols[c(3:10)]) +
theme(legend.position = c(.1,.8), legend.background = element_rect(fill = "transparent"))
### add violin plots
# major clades
# rename to avoid overwritting
gg.elm.prm.maj.clds <- gg.major.clade.collapse.tree
agg.elm.param.maj.clds <- agg.elm.param
# change species name
agg.elm.param.maj.clds$species <- agg.elm.param.maj.clds$Clade
# add facet violin
gg.elm.prm.maj.clds <- facet_plot(gg.elm.prm.maj.clds, panel = w, data = agg.elm.param.maj.clds, geom = geom_violin, aes(x= get(w), group = label), fill = adjustcolor("gray", alpha.f = 0.5)) +
theme(legend.position = c(.1,.9), legend.background = element_rect(fill = "transparent"))
# hummingbird clades
# rename to avoid overwritting
gg.elm.prm.hmm.clds <- gg.hmm.clade.collapse.tree
agg.elm.param.hmm.clds <- agg.elm.param.hmm
# change species name
agg.elm.param.hmm.clds$species <- agg.elm.param.hmm.clds$Clade
# add facet violin
gg.elm.prm.hmm.clds <- facet_plot(gg.elm.prm.hmm.clds, panel = w, data = agg.elm.param.hmm.clds, geom = geom_violin, aes(x= get(w), group = label), fill = adjustcolor("gray", alpha.f = 0.5)) +
theme(legend.position = "none", legend.background = element_rect(fill = "transparent"))
# plot both together
print(plot_grid(gg.elm.prm, gg.elm.prm.hmm, gg.elm.prm.maj.clds, gg.elm.prm.hmm.clds, ncol = 2))
}
## [1] "duration"

## [1] "meanfreq"

## [1] "sd"

## [1] "freq.median"

## [1] "freq.Q25"

## [1] "freq.Q75"

## [1] "freq.IQR"

## [1] "time.median"

## [1] "time.Q25"

## [1] "time.Q75"

## [1] "time.IQR"

## [1] "skew"

## [1] "kurt"

## [1] "sp.ent"

## [1] "time.ent"

## [1] "entropy"

## [1] "sfm"

## [1] "meandom"

## [1] "mindom"

## [1] "maxdom"

## [1] "dfrange"

## [1] "modindx"
